<template>
  <div class="resource-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 表单结构 -->
        <el-form
          :inline="true"
          :model="form"
          class="demo-form-inline"
          ref="form"
        >
          <el-form-item label="资源名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="资源名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="资源路径" prop="url">
            <el-input
              v-model="form.url"
              placeholder="资源路径"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="资源分类" prop="categoryId">
            <el-select
              v-model="form.categoryId"
              placeholder="资源分类"
              clearable
            >
              <!-- 请求接口进行下拉菜单项设置 -->
              <el-option
                v-for="item in resourceCategories"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="onReset"
            >重置</el-button>
            <el-button
              type="primary"
              :disabled="isLoading"
              @click="onSubmit"
            >查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="clearfix">
        <el-button @click="handleAdd">添加资源</el-button>
        <el-button
          @click="$router.push({
            name: 'resource-create'
          })"
        >资源分类</el-button>
        <el-divider></el-divider>
      </div>
      <div>
        <!-- 表格结构 -->
        <el-table
          :data="resources"
          style="width: 100%"
          v-loading="isLoading"
          >
          <el-table-column
            label="编号"
            type="index"
            width="100"
          >
          </el-table-column>
          <el-table-column
            prop="name"
            label="资源名称">
          </el-table-column>
          <el-table-column
            prop="url"
            label="资源路径">
          </el-table-column>
          <el-table-column
            prop="description"
            label="描述">
          </el-table-column>
          <!-- 这是过滤器需要使用作用域插槽获取数据 -->
          <el-table-column
            label="添加时间">
              <template slot-scope="scope">
                <span>{{ scope.row.createdTime | dataFormat }}</span>
              </template>
          </el-table-column>
          <el-table-column
            label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="handleEdit(scope.row)">编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)">删除</el-button>
              </template>
          </el-table-column>
        </el-table>
        <!-- 分页结构 -->
         <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="form.current"
            :page-sizes="[10, 15, 20]"
            :page-size="form.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            :disabled="isLoading"
            >
         </el-pagination>
      </div>
      <!-- 添加资源的对话框结构 -->
      <el-dialog
        :title="isEdit ? '编辑功能' : '添加功能'"
        :visible.sync="dialogVisible"
        width="30%">
        <!-- 将添加与编辑功能单词封装到组件中 -->
        <create-or-edit
          v-if="dialogVisible"
          :is-edit="isEdit"
          :resource-id="resourceId"
          @success="handleSuccess"
          @cancel="dialogVisible = false"
        ></create-or-edit>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import CreateOrEdit from './CreateOrEdit'
import { getResourcePages, getResourceCategories, deleteResource } from '@/services/resource'

export default {
  name: 'ResourceList',
  components: {
    CreateOrEdit
  },
  data () {
    return {
      // 用于储存资源列表数据
      resources: [],
      form: {
        // 当前显示页号
        current: 1,
        // 每页显示的数据条数
        size: 10,
        // 资源名称
        name: '',
        // 资源路径
        url: '',
        // 资源分类 ID
        categoryId: ''
      },
      // 数据总数
      totalCount: 0,
      // 存储资源分类信息
      resourceCategories: [],
      // 控制加载状态
      isLoading: false,
      // 控制对话框显示
      dialogVisible: false,
      // 控制对话框功能状态
      isEdit: false,
      // 储存 Id
      resourceId: ''
    }
  },
  created () {
    // 加载资源列表
    this.loadResources()
    // 加载资源分类列表
    this.loadResourceCategories()
  },
  methods: {
    handleAdd () {
      this.dialogVisible = true
      this.isEdit = false
    },
    // 监听子组件的添加状态，成功时触发
    handleSuccess () {
      // 隐藏对话框
      this.dialogVisible = false
      this.loadResources()
    },
    // 加载资源列表
    async loadResourceCategories () {
      const { data } = await getResourceCategories()
      if (data.code === '000000') {
        this.resourceCategories = data.data
      }
    },
    // 每页条数变化时触发
    handleSizeChange (val) {
      this.form.size = val
      // 修改了每页条数, 应还原默认值为 1
      this.form.current = 1
      this.loadResources()
    },
    // 页号改变触发
    handleCurrentChange (val) {
      this.form.current = val
      // 请求更新数据
      this.loadResources()
    },
    async loadResources () {
      // 开始加载数据
      this.isLoading = true
      const { data } = await getResourcePages(this.form)
      if (data.code === '000000') {
        this.resources = data.data.records
        this.totalCount = data.data.total
        // 取消加载状态
        this.isLoading = false
      }
    },
    // 编辑
    handleEdit (rowData) {
      this.dialogVisible = true
      this.isEdit = true
      this.resourceId = rowData.id
    },
    // 删除
    handleDelete (rowData) {
      this.$confirm('确认删除该资源吗？', '删除提示', { type: 'warning' })
        .then(async () => {
          const { data } = await deleteResource(rowData.id)
          console.log(data)
          if (data.code === '000000') {
            this.$message.success('删除成功')
            this.loadResources()
          }
        })
        .catch(() => {
          this.$message.info('取消删除')
        })
    },
    // 提交筛选功能
    onSubmit () {
      // 请求数据前，将请求的页数更新为 1
      this.form.current = 1
      this.loadResources()
    },
    // 重置按钮
    onReset () {
      this.$refs.form.resetFields()
    }
  },
  filters: {
    dataFormat (date) {
      date = new Date(date)
      return `
        ${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}
        ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}
      `
    }
  }
}
</script>

<style>

</style>
